@extends('layout')
@section('css')
<link rel="stylesheet" href="{{URL::asset('css/setPrize.css')}}">
@endsection
@section('content')
<div class="setPrize">
    <form class="prize_form" action="/addPrize" method="post">
        {{ csrf_field() }}
        <div class="type_box">
            <select class="type" name="type">
                <option value="0">首投红包</option>
                <option value="1">普通小红包</option>
                <option value="2">大红包</option>
            </select>
        </div>
        <div class="dropdown_box">
            <select class="dropdown">
                <option value="0">增加</option>
                @foreach($prizes as $value)
                    <option value="{{$value['id']}}">{{$value['prize_name']}}</option>
                @endforeach
            </select>
            <input type="hidden" name="prize_id" id="prize_id">
        </div>
        <div>
            <select name="prize_type" id="prize_type">
                <option value="0">红包</option>
                <option value="1">礼品</option>
            </select>
        </div>
        <div class="form-group">
            <label for="prize_name">奖品名称</label>
            <input type="text" class="form-control prize_name" placeholder="奖品名称" name="prize_name" id="prize_name">
        </div>
        <div class="form-group">
            <label for="amount">金额</label>
            <input type="text" class="form-control amount" placeholder="红包金额" name="amount" id="amount">
        </div>
        <div class="form-group">
            <label for="prize_probability">奖品概率</label>
            <input type="text" class="form-control prize_probability" placeholder="奖品概率" name="prize_probability" id="prize_probability">
        </div>
        <div class="form-group">
            <label for="quantity">奖品数量</label>
            <input type="text" class="form-control quantity" placeholder="奖品数量" name="quantity" id="quantity">
        </div>
        <button type="submit" class="btn btn-default add">添加</button>
    </form>
</div>
@endsection
@section('script')
    <script>
        (function(){
            $('.type').on('change',function(){
                var type = $('.type').val();
                 $.ajax({
                    url:'/getPrizeBytype',
                    data:{'type':type},
                    dataType:'JSON',
                    type:'get',
                    success:function(data){
                        var html;
                        html = '<option value="0">增加</option>'
                        data.data.forEach(function(index){
                            html += '<option value="'+index.id+'">'+index.prize_name+'</option>'
                        });
                        $('.dropdown').html(html);
                    }   
                });
            });
            $('.dropdown').on('change',function(){
                var id = $('.dropdown').val();
                $('#prize_id').val(id);
                var type = $('.type').val();
               if(id==0){
                   $('.prize_form').attr('action','/addPrize');
                    $('.add').text('添加');
                    $('.prize_name').val('');
                    $('.prize_probability').val('');
               }else{
                   $('.add').text('修改');
                   $('.prize_form').attr('action','/updatePrize');
                    $.ajax({
                        url:'/getPrizeByid',
                        data:{'prize_id':id,'type':type},
                        dataType:'JSON',
                        type:'get',
                        success:function(data){
                            $('.prize_name').val(data.data[0].prize_name);
                            $('.prize_probability').val(data.data[0].prize_probability);
                            $('.amount').val(data.data[0].amount);
                            $('#prize_type').val(data.data[0].prize_type);
                        }   
                    });
               }
            });
        })()
    </script>
@endsection
@if (session('status')=='success')
<script>
    alert('提交成功');
</script>
@elseif(session('status')=='error')
<script>
    alert('提交失败');
</script>
@endif